package chapter10;

public class Tax {
	
	public static final int SINGLE_FILER = 0;//单身纳税人
	public static final int MARRIED_JOINTLY = 1;//已婚共缴纳税人
	public static final int MARRIED_SEPARATELY = 2;//已婚单缴纳税人
	public static final int HEAD_OF_HOUSEHOLD = 3;//户主纳税人
	
	private int filingStatus;
	private int[][] breackets;/* = {
				{27050, 65550, 136750, 297350},
				{45200, 109250, 166500, 297350},
				{22600, 54625, 83250, 148675},
				{36250, 93650, 151650, 297350},//2001
				
				{8350, 33950, 82250, 171550, 372950},
				{16700, 67900, 137050, 20885, 372950},
				{8350, 33950, 68525, 104425, 186475},
				{11950, 45500, 117450, 190200, 372950}//2009
			};
	*/
	private double[] rates ;/*= {
				0.15, 0.275, 0.305, 0.355, 0.391,//2001
				0.10, 0.15, 0.25,  0.28, 0.33, 0.35//2009
				
			};*/
	private double taxableIncome;
	
	Tax(){
		/*创建以2001年的税率计算可征税收入为10000美元的单身纳税人的默认对象*/
		this(
				SINGLE_FILER,
				new int[][] {
						{27050, 65550, 136750, 297350},
						{45200, 109250, 166500, 297350},
						{22600, 54625, 83250, 148675},
						{36250, 93650, 151650, 297350}
					},
				new double[] {
						0.15, 0.275, 0.305, 0.355, 0.391
					},
				10000.0
			);
	}
	
	Tax(int filingStatus,int[][] breackets, double[] rates, double taxableIncome){
		this.filingStatus = filingStatus;
		this.breackets = breackets;
		this.rates = rates;
		this.taxableIncome = taxableIncome;
	}
	
	public int getFilingStatus(){
		return filingStatus;
	}
	
	public int[][] getBreackets(){
		return breackets;
	}
	
	public double[] getRates(){
		return rates;
	}
	
	public double getTaxableIncome(){
		return taxableIncome;
	}

	public void setFilingStatus(int status){
		filingStatus = status;
	}
	
	public void setBreackets(int[][] breackets){
		this.breackets = breackets;
	}
	
	public void setRates(double[] rates){
		this.rates = rates;
	}
	
	public void setTaxableIncome(double taxableIncome){
		this.taxableIncome = taxableIncome;
	}
	
	public double getTax(){
		double taxAmount, taxTmp;
		taxAmount =0.0;
		taxTmp = 0.0;
		int leve;
		leve = judgmentLeve();
		for (int i = 0; i <= leve; i++){
			if(i != leve){
				taxAmount += (breackets[filingStatus][i] - taxTmp) * rates[i];
				taxTmp = breackets[filingStatus][i];
			}
			else
				taxAmount += (taxableIncome - taxTmp) * rates[leve];
			
		}
		
		return taxAmount;
		
	}
	
	private int judgmentLeve(){
		int i;
		for( i = 0; i < breackets[filingStatus].length; i++){
			if (taxableIncome <= breackets[filingStatus][i]){
				return i;
			}
		}
		return i;
	}
	
	
}

abstract class DemoTax extends Tax{
	/*验证14_27的第5点*/
	protected DemoTax(){
		super();
	}
	
	public abstract double getTax();
}
